package admin

import (
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model"
	"time"
)

type AdminQrTicket struct {
	ID         uint `gorm:"primarykey;->"`
	Ticket     string
	AdminId    uint
	Confirmed  string
	ExpireTime string
	model.BaseTime
}

// var one AdminQrTicket 清空过期数据
func (AdminQrTicket) Refresh() error {

	var timeNowStr = time.Now().Format(time.DateTime)

	if err := bean.MysqlDb.Where("`expire_time`<?", timeNowStr).Limit(100).Delete(&AdminQrTicket{}).Error; err != nil {
		return err
	}

	return nil
}

// 获取 ticket 十分钟有效期
func (AdminQrTicket) New(ticket string) (string, error) {

	var expireTimeStr = time.Now().Add(time.Minute * 10).Format(time.DateTime)

	var one AdminQrTicket

	one.Ticket = ticket
	one.ExpireTime = expireTimeStr

	if err := bean.MysqlDb.Create(&one).Error; err != nil {
		bean.Log.Error(err)
		return "", err
	}

	return one.Ticket, nil
}
